package com.qn.visitor.analyze.ipaddressr.strategy.ipwhois;

import com.alibaba.fastjson2.JSON;
import com.qn.visitor.analyze.ipaddressr.strategy.IpAddressrStrategy;
import com.qn.visitor.model.UserVisitorRequest;
import com.qn.visitor.model.bo.analyze.IpAddressModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Component
public class IpwhoisAnalyze implements IpAddressrStrategy {
    @Autowired
    IpwhoisProperties properties;
    @Override
    public IpAddressModel analyze(UserVisitorRequest ubrBo) throws Exception {
        IpwhoisModel ipwhoisModel= null;
        try {
            ipwhoisModel = getIpwhoisPro(ubrBo.getClientIp());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if(ipwhoisModel==null||ipwhoisModel.getSuccess().equals(false)){
            return null;
        }
        //获取数据
        IpAddressModel ipAddressrModel=new IpAddressModel();
        ipAddressrModel.setIp(ipwhoisModel.getIp());
        ipAddressrModel.setContinent(ipwhoisModel.getContinent());
        ipAddressrModel.setContinentCode(ipwhoisModel.getContinentCode());
        ipAddressrModel.setCountry(ipwhoisModel.getCountry());
        ipAddressrModel.setCountryCode(ipwhoisModel.getCountryCode());
        ipAddressrModel.setRegion(ipwhoisModel.getRegion());
        ipAddressrModel.setRegionCode(ipwhoisModel.getRegionCode());
        ipAddressrModel.setCity(ipwhoisModel.getCity());
        ipAddressrModel.setLatitude(ipwhoisModel.getLatitude());
        ipAddressrModel.setLongitude(ipwhoisModel.getLongitude());
        ipAddressrModel.setConnectionAsn(ipwhoisModel.getConnection().getAsn());
        ipAddressrModel.setConnectionOrg(ipwhoisModel.getConnection().getOrg());
        ipAddressrModel.setTimezoneId(ipwhoisModel.getTimezone().getId());
        ipAddressrModel.setTimezoneCurrentTime(ipwhoisModel.getTimezone().getCurrentTime());
        ipAddressrModel.setSecurityAnonymous(ipwhoisModel.getSecurity().getAnonymous() ? 1 : 0);
        ipAddressrModel.setSecurityProxy(ipwhoisModel.getSecurity().getProxy() ? 1 : 0);
        ipAddressrModel.setSecurityVpn(ipwhoisModel.getSecurity().getVpn() ? 1 : 0);
        ipAddressrModel.setSecurityTor(ipwhoisModel.getSecurity().getTor() ? 1 : 0);
        ipAddressrModel.setSecurityHosting(ipwhoisModel.getSecurity().getHosting() ? 1 : 0);
        return ipAddressrModel;
    }
    public  IpwhoisModel getIpwhoisPro(String str) throws Exception {
        URL url = new URL(properties.getApiUrl().replace("{ip}",str));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BufferedReader reader;
        int code = connection.getResponseCode();
        if (code == 200) {
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } else {
            reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        }
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        return JSON.parseObject(response.toString(),IpwhoisModel.class);
    }
}
